package com.meilele.controller.czh;

import com.meilele.biz.czh.CompanyBiz;
import com.meilele.biz.czh.FiveDesignBiz;
import com.meilele.biz.czh.FivePostBiz;
import com.meilele.entity.czh.FiveCompany;
import com.meilele.entity.czh.FiveDesign;
import com.meilele.entity.czh.FivePost;
import com.meilele.entity.czh.vo.FiveComDes;
import com.meilele.entity.czh.vo.FiveRcpVo;
import com.meilele.entity.zhengchao.AjaxMsgModel;
import com.meilele.util.zhengchao.Contants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/five/manager")
public class FiveManagerController {
    @Resource
    private CompanyBiz companyBiz;
    @Resource
    private FiveDesignBiz fiveDesignBiz;
    @Resource
    private FivePostBiz fivePostBiz;
    @RequestMapping("/index")
    public String index(){
        return "/five/manager/index";
    }
    @RequestMapping("/listCompany")
    public String listCompany(Model model,int pageNum){
        List<FiveCompany> fiveCompanyList=companyBiz.findAll(pageNum);

        int totalCount=companyBiz.findcon();
        model.addAttribute("fiveCompanyList",fiveCompanyList);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("pageNum",pageNum);
        return "/five/manager/listCompany";
    }
    @RequestMapping("/preAddCompany")
    public String preAddCompany(){
        return "/five/manager/addCompany";
    }
    @ResponseBody
    @RequestMapping("/addCompany")
    public AjaxMsgModel addCompany(FiveCompany fiveCompany,MultipartHttpServletRequest request)throws Exception{
        MultipartFile file = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        File newFile = new File(Contants.IMAGE_PATH + fileName);
        file.transferTo(newFile);
        MultipartFile img = request.getFile("photo2");
        String imgxml = UUID.randomUUID().toString();
        File newFilexml = new File(Contants.IMAGE_PATH + imgxml);
        img.transferTo(newFilexml);
        fiveCompany.setCompanyLogo(fileName);
        fiveCompany.setCompanyImg(imgxml);
        companyBiz.save(fiveCompany);
        return new AjaxMsgModel("200","添加成功","czhlist","","closeCurrent","");
    }
    @ResponseBody
    @RequestMapping("/deleteCompany")
    public AjaxMsgModel deleteCompany(int companyId){
        companyBiz.deleteCompany(companyId);
        return new AjaxMsgModel("200","删除成功","czhlist","","closeCurrent","");
    }
    @RequestMapping("/preUpdate")
    public String preUpdate(int companyId,Model model){
        FiveCompany fiveCompany=companyBiz.findId(companyId);
        List<FiveCompany> companyList=companyBiz.findAllcom();
        model.addAttribute("CompanyList",companyList);
        model.addAttribute(fiveCompany);
        return "/five/manager/updateCompany";
    }
    @ResponseBody
    @RequestMapping("/updateCompany")
    public AjaxMsgModel updateCompany(FiveCompany fiveCompany,MultipartHttpServletRequest request)throws Exception{
        MultipartFile file = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        File newFile = new File(Contants.IMAGE_PATH + fileName);
        file.transferTo(newFile);
        fiveCompany.setCompanyLogo(fileName);
        MultipartFile img = request.getFile("photo2");
        String imgxml = UUID.randomUUID().toString();
        File newFilexml = new File(Contants.IMAGE_PATH + imgxml);
        img.transferTo(newFilexml);
        fiveCompany.setCompanyImg(imgxml);
        companyBiz.updateCompany(fiveCompany);
        return new AjaxMsgModel("200","修改成功","czhlist","","closeCurrent","");
    }
    @RequestMapping("/listDesign")
    public String listDesign(int pageNum,Model model){
        List<FiveComDes>fiveCom=fiveDesignBiz.findCom(pageNum);
        int num=fiveDesignBiz.findcont();
        model.addAttribute("fiveCom",fiveCom);
        model.addAttribute("num",num);
        model.addAttribute("pageNum",pageNum);
        return "/five/manager/listDesign";
    }

    @RequestMapping("/preAddDesign")
    public  String preAddDesign(Model model){
        List<FiveCompany> companyList=companyBiz.findAllcom();
        model.addAttribute("companyList",companyList);
        return "/five/manager/addDesign";
    }
    @ResponseBody
    @RequestMapping("/addDesign")
    public AjaxMsgModel addDesign(FiveDesign fiveDesign,MultipartHttpServletRequest request)throws  Exception{

        MultipartFile file = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        File newFile = new File(Contants.IMAGE_PATH + fileName);
        file.transferTo(newFile);
        fiveDesign.setDesignLogo(fileName);
        fiveDesignBiz.save(fiveDesign);
        return new AjaxMsgModel("200","添加成功","czhlist","","closeCurrent","");
    }
    @ResponseBody
    @RequestMapping("/deleteDesign")
    public AjaxMsgModel deleteDesign(int designId){
         fiveDesignBiz.deleteDesign(designId);
        return new AjaxMsgModel("200","删除成功","czhlist","","closeCurrent","");
    }
    @RequestMapping("/preUpdateDesign")
    public String preUpdateDesign(Model model,int designId){
        FiveDesign fiveDesign=fiveDesignBiz.findId(designId);
        List<FiveCompany> companyList=companyBiz.findAllcom();
        model.addAttribute("companyList",companyList);
        model.addAttribute("fivedesign",fiveDesign);
        return "/five/manager/updateDesign";
    }
    @ResponseBody
    @RequestMapping("/updateDesign")
    public AjaxMsgModel updateDesign(FiveDesign fiveDesign,MultipartHttpServletRequest request)throws Exception{
        MultipartFile file = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        File newFile = new File(Contants.IMAGE_PATH + fileName);
        file.transferTo(newFile);
        fiveDesign.setDesignLogo(fileName);
        fiveDesignBiz.updateDesign(fiveDesign);
        return new AjaxMsgModel("200","修改成功","czhlist","","closeCurrent","");
    }
    @RequestMapping("/listPost")
    public String listPost(Model model,int pageNum){
        List<FivePost> postList=fivePostBiz.findAllpost(pageNum);
        for(FivePost fivePost:postList){
            Date date = fivePost.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String stringDate = format.format(date);
            fivePost.setStringDate(stringDate);
        }
        int num=fivePostBiz.findCountasd();
        model.addAttribute("postList",postList);
        model.addAttribute("num",num);
        model.addAttribute("pageNum",pageNum);
        return "/five/manager/listPost";
    }
    @RequestMapping("/postDestail")
    public String postDetail(int postId,Model model){
        FiveRcpVo fiveRcpVo=fivePostBiz.findRcp(postId);
        model.addAttribute("fiveRcpVo",fiveRcpVo);
        return "/five/manager/postDestail";
    }
}
